﻿using System.Collections.Generic;
using System.Xml;

namespace IniFileManager
{
	/// <summary>
	/// Class representing inner structure of an enum element
	/// within property defined by library user in ini schema.
	/// </summary>
	internal sealed class IniEnumElementSchema : IniElementSchema
	{
		private readonly string defaultValue;
		private readonly List<string> enumerations;

		/// <summary>
		/// Creates "IniEnumElementSchema" from xml node in ini schema xml representation.
		/// </summary>
		/// <param name="enumElementNode">Ini enum element schema xml node.</param>
		internal IniEnumElementSchema( XmlNode enumElementNode )
		{
			// Embedded "IniSchema.xsd"'s definition guarantees existence and format of this attribute:
			this.defaultValue = enumElementNode.Attributes[IniSchemaXsd.ATTRIBUTE_DEFAULT].Value;
			this.enumerations = new List<string>();

			XmlNodeList enumerationNodes = enumElementNode.SelectNodes( IniSchemaXsd.ELEMENT_ENUMERATION );
			foreach( XmlNode enumerationNode in enumerationNodes )
			{
				// Embedded "IniSchema.xsd"'s definition guarantees existence of InnerText within enumerationNode:
				enumerations.Add( enumerationNode.InnerText );
			}
		}

		/// <summary>
		/// Boxed default string value of an element into object.
		/// </summary>
		internal override object DefaulValue
		{
			get
			{
				return this.defaultValue;
			}
		}

		/// <summary>
		/// Tells wheather a value is valid within.
		/// </summary>
		/// <param name="boxedValue">Value boxed in object.</param>
		/// <returns>True if value valid within. False if not.</returns>
		internal override bool IsValueValidWithin( object boxedValue )
		{
			string value = (string)boxedValue;
			if( enumerations.Contains( value ) )
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
}
